/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.purchaseView;
/**
 * 说明：
 * 进货查询窗口
 * @Author：黄盛强
 * @Date：2012-03-26
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TPurchase;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.JPanelGridLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundGray;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.PurchaseEnterModel;
import com.ruipengkj.service.TEmployeeService;
import com.ruipengkj.service.TPurchaseService;
import com.ruipengkj.view.base.BasePagePanel;

public class PurchaseEnterPanel extends BasePagePanel<TPurchase> implements  Runnable{

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent orderID; //查询进货单【输入采购单编号】
	private JLabelJTextFieldComponent employee = null; //采购员
	private DataPackageComponent startTime;//开始时间
	private DataPackageComponent endTime;//结束时间
	
	private JButton queryButton;//查询
	private JButton clearButton;//清空
	private JButton showButton; //查看详细
	private JButton addButton; //添加进货单
	
	private JTable table;
	private JScrollPane tableJsp;
	private PurchaseEnterModel purchaseEnterModel;
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TPurchase> pageView;//翻页主键
	private TPurchaseService purchaseService;
	
	private PurchaseEnterAddOrModifyPanel purchaseEnterAddPanel ;//添加或修改详细面板
	private TEmployeeService tEmployeeService;
	public PurchaseEnterPanel() {
		purchaseService = BuildSpringApplication.getInstance().getTPurchaseServiceImpl();
		tEmployeeService = BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	private void init() {
		pageView = new PageView<TPurchase>();
		purchaseEnterModel = new PurchaseEnterModel();
		table=uiFactory.createRPJTable(purchaseEnterModel, null, this, 30, JLabel.CENTER);
//		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		this.add(initTop(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(initTail(), BorderLayout.SOUTH);
		this.setVisible(true);
	}
	private JPanel initTop() {
		QueryPanel queryPanel=new QueryPanel("采购进货",ImageFactory.loadImage(ImageFactory.FUN_4_ICON_1));
		JPanel top_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		orderID = new JLabelJTextFieldComponent("进货单号：", 20);
		employee = new JLabelJTextFieldComponent("采购员：", 20);
		startTime = new DataPackageComponent("开始时间", 188);
		endTime = new DataPackageComponent("结束时间", 188);
		queryButton = new RPJButtonBlue("查询");
		clearButton= new RPJButtonGray("清空");
		top_panel.add(orderID);
		top_panel.add(employee);
		top_panel.add(startTime);
		top_panel.add(endTime);
		top_panel.add(queryButton);
		top_panel.add(clearButton);
		
		queryPanel.addPanel(top_panel);
//		queryPanel.addBoxPanel(top_panel);
		return queryPanel;
	}
	
	private JPanel initTail() {
		addButton = new RPJButtonRoundBlue("添加(Ctrl+A)");
		showButton= new RPJButtonRoundGray("查看详细");
		
		JPanel jp2 = new JPanelFlowLayoutComponent(new Component[]{new JLabel("   "),addButton,showButton}, FlowLayout.LEFT);
		JPanel tail_panel = new JPanelGridLayoutComponent(new Component[]{jp2,pagingComponent}, 1, 2);
		return tail_panel;
	}
	
	private void initListener(){
		clearButton.addActionListener(this);
		queryButton.addActionListener(this);
		addButton.addActionListener(this);
		showButton.addActionListener(this);
		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TPurchase tPurchase=new TPurchase();
		tPurchase.setPurchaseID(orderID.getText().getText().trim());
		tPurchase.setBuyer(employee.getText().getText().trim());
		tPurchase.setHappenTime(startTime.getDate());
		tPurchase.setEndTime(endTime.getDate());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					List<TEmployee> tEmployeeList = null;
					if (employee.getText().getText().trim() != null && !employee.getText().getText().trim().equals("")) {
						TEmployee tEmployee = new TEmployee();//根据名字查出退货人ID，
						tEmployee.setName(employee.getText().getText().trim());
						try {
							tEmployeeList = tEmployeeService.findService(tEmployee);
						} catch (Exception e1) {
							e1.printStackTrace();
						}
						if (tEmployeeList == null || tEmployeeList.size() <= 0) {
							tableJsp.getViewport().remove(waitComponent);
							tableJsp.getViewport().add(table);
							purchaseEnterModel.clear();
							return;
						}
					}
					
					pageView.setCurrentPage(currentPage);
					PageModel<TPurchase> pageModel = purchaseService.getScrollListService(pageView.getFirstResult(), pageView.getMaxResult(),tPurchase, tEmployeeList);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						purchaseEnterModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						purchaseEnterModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TPurchase> getPageView() {
		return pageView;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == queryButton) {
			//查询按钮
			this.termQuery(1);
		} else if(e.getSource() == addButton) {
			//添加按钮
			purchaseEnterAddPanel = new PurchaseEnterAddOrModifyPanel(null, "添加采购数据", true);
			purchaseEnterAddPanel.showDialog(null);
			if(purchaseEnterAddPanel.isSuccess){
				this.termQuery(1);
			}
		}else if(e.getSource()==showButton){
			//查看详细
			if(table.getSelectedRow()!=-1){
				String purchaseID = (String)table.getValueAt(table.getSelectedRow(), 0);
				purchaseEnterAddPanel = new PurchaseEnterAddOrModifyPanel(null, "采购数据详细", true);
				purchaseEnterAddPanel.showDialog(purchaseID);
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==clearButton){
			//清空
			this.clear();
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == table && e.getClickCount() == 2) {
			showButton.doClick();
		}
	}
	@Override
	public void clear() {
		purchaseEnterModel.clear();
		orderID.getText().setText("");
		employee.getText().setText("");
		startTime.setDate(null);
		endTime.setDate(null);
		
	}
}